package com.cy;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//JVM:
//-xx:+TranceClassLoading
@Slf4j
@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {

       SpringApplication.run(ProviderApplication.class, args);

    }

    //注解用于告诉sprig，一般配置中心数据发生变化，自动重新创建它描述的类的实例
    @RefreshScope
    @RestController
    public class ProviderController{

        @Value("${server.tomcat.threads.max:200}")
        private Integer maxThread;

        @RequestMapping("/provider/doGetMaxThread")
        public String doGetMaxThread(){
            return "server.threads.max is  "+maxThread;
        }

        //private static final Logger log= LoggerFactory.getLogger(ProviderApplication.class);

        public ProviderController(){
            System.out.println("ProviderController()");
        }

        //此属性会在对象构建是初始化
        //服务启动时会将所以配置信息（配置文件或配置中心）读取到Environment对象
        //@Value用于告诉spring从Environment对象中读取配置信息
        //将读取到配置内容赋值给对象的属性
        @Value("${logging.level.com.cy:debug}")
        private String logLevel;

        @GetMapping("/provider/doGetLogLevel")
        public String doGetLogLevel(){
            log.trace("==log.trace==");//跟踪
            log.debug("==log.debug==");//调试
            log.info("==log.info==");//常规信息
            log.warn("==log.warn==");//警告
            log.error("==log.error==");//错误信息
            return "log level is "+logLevel;
        }

        //@Value用于读取application.yml中的配置
        //
        @Value("${server.port:8080}")
        private String server;

        /**
         * 基于此方法实现一个字符串的回显
         * echo:回显
         * rest：一种软件架构编码风格，可以基于这种风格定义url*/
        @GetMapping("/provider/echo/{msg}")
        public String doRestEcho1(@PathVariable("msg") String msg) throws InterruptedException {


            //Thread.sleep(3000);
            //level: error
            //需求：通过配置中心中动态日志级别配置，控制日志信息的输出
            //通常日志级别：trace<debug<info<warn<error
            //很多系统的默认日志级别是info，调适程序是经常会用debug
            log.info("doRestEcho1 start {}",//{}表示占位符
                        System.currentTimeMillis());
            return server+ "say hello " + msg;

        }

    }

}
